package com.tang.crm.controller;

import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.dto.CustomerDto;
import com.tang.crm.entity.Customer;
import com.tang.crm.service.CusDevPlanService;
import com.tang.crm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 客户管理
 * @author SHIRELY阳
 */
@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    /**
     * 进入客户信息管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "customer/customer";
    }

    /**
     * 进入客户信息更新/修改界面
     * @return
     */
    @RequestMapping("/toAddOrUpdateCustomerPage")
    public String toAddOrUpdateCustomerPage(Integer id, HttpServletRequest request) {
        if (id != null) {
            // 将客户信息设置到请求域中
            request.setAttribute("customer", customerService.getById(id));
        }
        return "customer/add_update";
    }

    /**
     * 打开客户的订单页面
     * @param customerId
     * @param request
     * @return
     */
    @RequestMapping("/toCustomerOrderPage")
    public String toCustomerOrderPage(Integer customerId, HttpServletRequest request){
        // 通过客户ID查询客户记录，设置到请求域中
        request.setAttribute("customer", customerService.getById(customerId));
        return "customer/customer_order";
    }

    /**
     * 分页条件查询客户列表
     * @param customerDto
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> selectAllByParams(CustomerDto customerDto){
        return customerService.selectAllByParams(customerDto);
    }

    /**
     * 添加客户信息
     * @param customer
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addCustomer(Customer customer){
        customerService.addCustomer(customer);
        return success("添加客户信息成功");
    }

    /**
     * 更新客户信息
     * @param customer
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateCustomer(Customer customer){
        customerService.updateCustomer(customer);
        return success("更新客户信息成功");
    }

    /**
     * 删除客户信息
     * @param customerId
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteCustomer(@RequestParam("id") Integer customerId){
        customerService.deleteCustomer(customerId);
        return success("删除客户信息成功");
    }

    /**
     * 客户贡献分析
     * @param customerDto
     * @return
     */
    @RequestMapping("/queryCustomerContributionByParams")
    @ResponseBody
    public Map<String, Object> selectCustomerContributionByParams(CustomerDto customerDto) {
        return customerService.selectCustomerContributionByParams(customerDto);
    }

    /**
     * 查询客户构成 （折线图）
     * @return
     */
    @RequestMapping("/countCustomerMake")
    @ResponseBody
    public Map<String, Object> countCustomerMake() {
        return customerService.countCustomerMake();
    }

    /**
     * 查询客户构成 （饼状图）
     * @return
     */
    @RequestMapping("/countCustomerMake02")
    @ResponseBody
    public Map<String, Object> countCustomerPieGraph() {
        return customerService.countCustomerPieGraph();
    }
}
